import pygame

#This class holds all of the components of the heads up display
class HUD:
    def __init__(self, screen):
        self.fps = label(screen, (10,10), "FPS:")
        self.health_bar = status_bar(screen, (250,18))
        self.health_bar.setText("Health")
        self.hunger_bar = ranged_status_bar(screen, (400,18))
        self.hunger_bar.setText("Hungry")
        self.kills = label(screen, (575,10), "Kills:")
        
        self.hunger_bar.addRange(0,20,"Starving",(255,0,0))
        self.hunger_bar.addRange(21,79,"Hungry",(0,255,0))
        self.hunger_bar.addRange(80,100,"Full/Heartburn",(0,0,255))
        self.fps_on = True
        
        self.goal = label(screen, (575,25), "Goal:")
        
    def update(self, kills, fps, health, hunger, goal, goalType):
        self.updateGoal(goal, goalType)
        self.updateKills(kills)
        self.updateFPS(fps)
        self.updateHealth(health)
        self.updateHunger(hunger)
    def updateGoal(self, goal, goalType):
        self.goal.setValue("Goal: " + str(goal) + " " + goalType)
    def updateKills(self, kills):
        self.kills.setValue("Kills: " + str(kills))
    def updateFPS(self, fps):
        self.fps.setValue("FPS: " + str(fps)[0:4])
    def updateHealth(self, health):
        self.health_bar.setValue(health)
    def updateHunger(self, hunger):
        self.hunger_bar.setValue(hunger)
    def draw(self):
        self.health_bar.draw()
        self.hunger_bar.draw()
        if self.fps_on:
            self.fps.draw()
        self.kills.draw()
        self.goal.draw()
        
    def toggleFPS(self):
        self.fps_on = not self.fps_on

#this is a base class for gui components
class widget:
    def __init__(self, screen, pos):
        self.screen = screen
        self.position = pos #(x,y)
    
class Background(widget):
    BACKGROUND_COLOR = (120,200,120) #midtone green
    def __init__(self, screen, pos, size, background_color = BACKGROUND_COLOR):
        widget.__init__(self, screen, pos)
        self.background_color = background_color
        self.background = pygame.Surface(size)
        self.background = self.background.convert()
        self.background.fill(self.background_color)

    def draw(self):
        self.screen.blit(self.background,self.position)
    
#static text components
class label(widget):
    def __init__(self, screen, pos, text=""):
        widget.__init__(self, screen, pos)
        self.textColor = (0,0,0)
        self.size = 20
        self.text = text
        self.show_background = False
        self.backColor = None
        self.font = pygame.font.Font(None,self.size)
        self.render()
    def render(self):
        if self.backColor != None:
            self.rendered_text = self.font.render(self.text,1,self.textColor, self.backColor)
        else:
            self.rendered_text = self.font.render(self.text,1,self.textColor)
        self.rendered_pos = self.rendered_text.get_rect()
        self.rendered_pos.topleft = self.position
        self.reRender = False
    def setValue(self, text):
        self.text = text
        self.reRender = True
    def draw(self):
        if self.reRender:
            self.render()
        self.screen.blit(self.rendered_text, self.rendered_pos)
    def setTextColor(self, color):
        self.textColor = color
        self.reRender = True
    def setBackColor(self, color):
        self.backColor = color
        self.reRender = True
    def setPosition(self, position):
        self.position = position
        self.reRender = True
    def setSize(self, size):
        self.size = size
        self.font = pygame.font.Font(None,self.size)
        self.reRender = True

class status_bar(widget):
    def __init__(self, screen, pos):
        widget.__init__(self, screen, pos)
        self.amount = 100
        (x,y) = pos
        self.label = label(screen, (x, y-15))
        self.backcolor = (0,0,0)
        self.emptycolor = (255,0,0)
        self.color = (0,255,0)
        self.labelcolor = (0,0,0)
    def draw(self):
        backgroundBar = pygame.Surface((110,20))
        backgroundBar = backgroundBar.convert()
        backgroundBar.fill(self.backcolor)

        bar = pygame.Surface((self.amount, 10))
        bar = bar.convert()
        bar.fill(self.color)
        
        emptyBar = pygame.Surface((100 - self.amount,10))
        emptyBar = emptyBar.convert()
        emptyBar.fill(self.emptycolor)

        backgroundBar.blit(bar,(5,5))
        backgroundBar.blit(emptyBar,(5 + self.amount, 5))

        self.screen.blit(backgroundBar, self.position)
        self.label.draw()
    def setValue(self, amount):
        if amount < 0: amount = 0
        if amount > 100: amount = 100
        self.amount = amount
    def setText(self, text):
        self.label.setValue(text)
    def setColor(self, color):
        self.color = color
    def setLabelTextColor(self, color):
        self.label.setTextColor(color)

class ranged_status_bar(status_bar):
    def __init__(self, screen, pos):
        status_bar.__init__(self, screen, pos)
        self.ranges = []
        self.range_text = []
        self.range_color = []
    def setValue(self, amount):
        status_bar.setValue(self, amount)
        i = 0
        for (x,y) in self.ranges:
            if amount >= x and amount <= y:
                self.setText(self.range_text[i])
                self.setLabelTextColor(self.range_color[i])
            i+=1
    def addRange(self, minimum, maximum, text, color):
        self.ranges.append((minimum,maximum))
        self.range_text.append(text)
        self.range_color.append(color)
